package com.zhangxx.web.service.imp;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhangxx.web.common.model.PageResult;
import com.zhangxx.web.domain.TBlogArticle;
import com.zhangxx.web.domain.TBlogArticleExample;
import com.zhangxx.web.mapper.TBlogArticleMapper;
import com.zhangxx.web.service.BlogAricleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zhangbo
 * @date 2019-08-15
 */
@Service
@CacheConfig( cacheNames = "BlogAricleServiceImpl")
public class BlogAricleServiceImpl implements BlogAricleService {

    @Autowired
    private TBlogArticleMapper tBlogArticleMapper;
//key="'id:'+#id+':pageNum:'+#pageNum+':pageSize:'+#pageSize+':getContent:'+#getContent",unless="#result==null"
    @Override
    @Cacheable
    public PageResult list(String id, Integer pageNum, Integer pageSize,Boolean getContent) {

        if (null==pageNum||0>pageNum){
            pageNum=1;
        }
        if(null==pageSize||0>pageSize){
            pageSize=15;
        }

        PageHelper.startPage( pageNum,  pageSize).setOrderBy(" created DESC ");

        TBlogArticleExample tBlogArticleExample= new TBlogArticleExample();
        TBlogArticleExample.Criteria criteria = tBlogArticleExample.createCriteria();
        if (StringUtils.isNotBlank(id)){
            criteria.andCidEqualTo(Integer.valueOf(id));
        }
        if (getContent == null) {
            getContent=false;
        }
//        tBlogArticleExample.setOrderByClause(" created DESC");
        List<TBlogArticle> blogArticleList = getContent?tBlogArticleMapper.selectByExampleWithBLOBs(tBlogArticleExample):tBlogArticleMapper.selectByExample(tBlogArticleExample);
        PageInfo<TBlogArticle> pageInfo = new PageInfo<>(blogArticleList);

        return new PageResult(pageInfo.getTotal(),pageInfo.getList());

    }


}
